<?php

namespace app\admin\controller\kuyou;

use app\common\controller\Backend;
use Exception;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Session;

/**
 * 验收单
 *
 * @icon fa fa-circle-o
 */
class Acceptance extends Backend
{
    /**
     * Procedure模型对象
     * @var \app\admin\model\kuyou\Acceptance
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\kuyou\Acceptance;
        $setData = (new Goods())->getSetData();
        $this->assign('set', $setData['setList']);
        $this->assignconfig('set', $setData['setList']);
        $this->assign('nowmenu', $setData['nowmenu']);

        $stores_list = Db::name('kuyou_stores')->where('shop_id', $this->shop_id)->where(['status' => '1'])->select();
        $this->view->assign("storesList", $stores_list);
        $this->assignconfig("storesList", $stores_list);
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index($ids = null)
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where('acceptance.shop_id', $this->shop_id)
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $list_k => $list_v) {
                $list[$list_k]['workshop_id'] = Db::name('kuyou_workshop')->where('shop_id', $this->shop_id)->where(['id' => $list_v['workshop_id']])->column('name');
                if ($list_v['code_type'] == 1) {
                    $production_id = Db::name('kuyou_production')->where('shop_id', $this->shop_id)->where(['purchase_code' => $list_v['chase_code']])->value('id');
                    $goods_list = Db::name('kuyou_production_info')->where('shop_id', $this->shop_id)->where(['production_id' => $production_id, 'type' => 2])->field('goods_name,number')->select();
                } else {
                    $plan = Db::name('kuyou_outsourcing')->where('shop_id', $this->shop_id)->where(['purchase_code' => $list_v['chase_code']])->find();
                    if ($plan) {
                        $production_id = Db::name('kuyou_plan')->where('shop_id', $this->shop_id)->where(['purchase_code' => $plan['plan_code']])->value('id');
                        $goods_list = Db::name('kuyou_plan_info')->where('shop_id', $this->shop_id)->where(['plan_id' => $production_id])->field('goods_name,number')->select();
                    } else {
                        $goods_list = [];
                    }
                }
                $str = '';
                foreach ($goods_list as $goods_list_k => $goods_list_v) {
                    $str .= $goods_list_v['goods_name'] . '*' . $goods_list_v['number'] . ' ';
                }
                $list[$list_k]['goods_info'] = $str;
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        $this->assignconfig('ids', $ids);

        $synchronization = Db::name('kuyou_sets')->where('shop_id', $this->shop_id)->where(['name' => 'synchronization'])->value('is_checked');//同步设置
        $prefix = config('database.prefix');
        $table = \think\Db::query("show tables like '" . $prefix . "kuyou_goods'");
        if (empty($table)) {
            $synchronization = 0;
        }
        $this->view->assign("synchronization", $synchronization);
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $examine = Db::name('kuyou_sets')->where('shop_id', $this->shop_id)->where(['name' => 'examine'])->value('is_checked');
                foreach ($params['goods_code'] as $k => $v) {
                    if (!empty($v)) {//有产品编号
                        $status = true;
                    }
                }
                if (!isset($status)) {
                    $this->error('请填写详情数据');
                }
                $code_type = isset($params['code_type']) ? $params['code_type'] : 1;
                $code_type = $code_type == 2 ? 2 : 1;

                $set = Db::name('kuyou_sets')->where('shop_id', $this->shop_id)->where(['name' => 'serial'])->value('is_checked');
                $batch = Db::name('kuyou_sets')->where('shop_id', $this->shop_id)->where(['name' => 'batch'])->value('is_checked');
                $data = [];
                $number = 0;
                $all_money = 0;
                $finish_number = 0;
                foreach ($params['goods_code'] as $k => $v) {
                    if (!empty($v)) {//有产品编号
                        $number += (float)$params['need_number'][$k];
                        $all_money += (float)$params['all_money'][$k];
                        $goods = Db::name('kuyou_goods')->where('shop_id', $this->shop_id)->where(['goods_code' => $v])->field('id,goods_name,serial,batch,auxiliary')->find();
                        if (!isset($params['stores_id'][$k]) || empty($params['stores_id'][$k])) {
                            $this->error('请选择' . $goods['goods_name'] . '仓库');
                        }
                        if ($goods['auxiliary'] == 1 && empty($params['goods_attr'][$k])) {
                            $this->error('请选择' . $goods['goods_name'] . '属性');
                        }

                        if ($goods['batch'] == 1 && $batch == 1) {
                            if (empty($params['batch'][$k])) {
                                $this->error('请选择' . $goods['goods_name'] . '生产批次');
                            }
                        }

                        $serial_code = [];
                        if (isset($params['period_period'][$k]) && $goods['serial'] == 1 && $set == 1) {
                            if (empty($params['period_period'][$k])) {
                                $this->error('请输入' . $goods['goods_name'] . '商品序列号');
                            }
                            $period_period = unserialize($params['period_period'][$k]);
                            foreach ($period_period as $period_period_k => $period_period_v) {
                                $serial_code[] = $period_period_v['serial'];
                            }
                            $where = [];
                            $where['serial'] = ['in', $serial_code];
                            if (!empty($params['purchase_id'][$k])) {
                                $where['purchase_code'] = $params['purchase_id'][$k];
                            }
                            $goods_serial = Db::name('kuyou_goods_serial')->where('shop_id', $this->shop_id)->where($where)->column('serial');
                            if (!empty($goods_serial)) {
                                $this->error($goods['goods_name'] . '商品序列号已使用');
                            }
                            $goods_serials = Db::name('kuyou_goods_serial')->where('shop_id', $this->shop_id)->where(['store_status' => '1', 'serial' => ['in', $serial_code]])->column('serial');
                            if (!empty($goods_serials)) {
                                $goods_serials = implode(',', $goods_serials);
                                $this->error('商品序列号' . $goods_serials . '已经在库，不可重复入库');
                            }
                        }
                        $finish_number += (float)$params['number'][$k];
                        $bad_number = ((float)$params['need_number'][$k]) - ((float)$params['number'][$k]);
                        $bad_number = $bad_number < 0 ? 0 : $bad_number;
                        $data[] = [
                            'shop_id' => $this->shop_id,
                            'type' => 2,
                            'goods_id' => $goods['id'],
                            'goods_code' => $v,
                            'goods_name' => $goods['goods_name'],
                            'goods_note' => $params['goods_note'][$k],
                            'unit_id' => (float)$params['goods_unit'][$k],
                            'default_unit' => (float)$params['default_unit'][$k],
                            'purchase' => (float)$params['unit_price'][$k],
                            'number' => (float)$params['number'][$k],
                            'basic_nmber' => (float)$params['basic_nmber'][$k],
                            'money' => (float)$params['all_money'][$k],
                            'stores_id' => (float)$params['stores_id'][$k],
                            'need_number' => (float)$params['need_number'][$k],
                            'bad_number' => $bad_number,
                            'goods_attr' => (float)$params['goods_attr'][$k],
                            'remark' => $params['remark'][$k],
                            'batch_id' => !empty($params['batch_id'][$k]) ? $params['batch_id'][$k] : null,
                            'batch_name' => !empty($params['batch'][$k]) ? $params['batch'][$k] : null,
                            'manufacture' => !empty($params['manufacture'][$k]) ? strtotime($params['manufacture'][$k]) : null,
                            'guarantee' => !empty($params['guarantee'][$k]) ? $params['guarantee'][$k] : null,
                            'validity' => !empty(strtotime($params['validity'][$k])) ? strtotime($params['validity'][$k]) : null,
                            'period' => $params['period_period'][$k] === '' ? null : $params['period_period'][$k],
                            'createtime' => time(),
                            'updatetime' => time(),
                        ];
                    }
                }

                if ($code_type == 1) {
                    $plan = Db::name('kuyou_production')->where('shop_id', $this->shop_id)->where(['purchase_code' => $params['chase_code']])->find();
                    if (empty($plan)) {
                        $this->error('生产单查询失败');
                    }
                } else {
                    $plan = Db::name('kuyou_outsourcing')->where('shop_id', $this->shop_id)->where(['purchase_code' => $params['chase_code']])->find();
                    if (empty($plan)) {
                        $this->error('委外加工单查询失败');
                    }
                }
                if (empty($data)) {
                    $this->error('请填写详情数据');
                } else {
                    $admin = Session::get('admin');
                    $chase_code = $params['chase_code'];
                    if ($code_type == 1) {
                        $production_code = $params['chase_code'];
                        $outsourcing_code = '';
                    } else {
                        $production_code = '';
                        $outsourcing_code = $params['chase_code'];
                    }
                    $row = [];
                    $row['shop_id'] = $this->shop_id;
                    $row['code_type'] = $code_type;
                    $row['chase_code'] = $chase_code;
                    $row['production_code'] = $production_code;
                    $row['outsourcing_code'] = $outsourcing_code;
                    $row['purchase_code'] = $params['purchase_code'];
                    $row['purchase_time'] = strtotime($params['purchase_time']);
                    $row['remark'] = $params['note'];
                    $row['work_id'] = $params['work_id'];
                    $row['number'] = $number;
                    $row['all_money'] = $all_money;
                    $row['finish_number'] = $finish_number;
                    $row['examine_status'] = '0';
                    $row['createtime'] = time();
                    $row['updatetime'] = time();
                    $row['preparation_id'] = $admin['id'];
                    $row['operation_id'] = $admin['id'];
                    if ($this->model->where('shop_id', $this->shop_id)->where(['purchase_code' => trim($params['purchase_code'])])->find()) {
                        $this->error('单据编号已占用');
                    }
                }

                $result = false;
                Db::startTrans();
                try {
                    if (!empty($row)) {
                        $result = $this->model->insertGetId($row);
                    }

                    foreach ($data as $data_k => $data_v) {
                        $data[$data_k]['production_id'] = $result;
                    }
                    Db::name('kuyou_acceptance_info')->insertAll($data);
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    if ($examine != 1) {
                        try {
                            $exa = $this->examine($result, 1);
                        } catch (Exception $e) {
                        }
                        if (!isset($exa) || $exa['status'] != 1) {
                            $exa = $this->examine($result, 1);
                            try {
                                $this->del($result);
                            } catch (Exception $e) {
                            }
                            $this->error($exa['msg']);
                        }
                    }
                    $this->success('添加成功');
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $code = (new Goods())->getCodes('', 36);
        $this->view->assign("code", $code);
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $examine = Db::name('kuyou_sets')->where('shop_id', $this->shop_id)->where(['name' => 'examine'])->value('is_checked');
                if ($row['examine_status'] == 1 && $examine == 1) {
                    $this->error('不能编辑已审核的数据');
                }
                $params = $this->preExcludeFields($params);
                $examine = Db::name('kuyou_sets')->where('shop_id', $this->shop_id)->where(['name' => 'examine'])->value('is_checked');
                foreach ($params['goods_code'] as $k => $v) {
                    if (!empty($v)) {//有产品编号
                        $status = true;
                    }
                }
                if (!isset($status)) {
                    $this->error('请填写详情数据');
                }
                $code_type = isset($params['code_type']) ? $params['code_type'] : 1;
                $code_type = $code_type == 2 ? 2 : 1;

                $set = Db::name('kuyou_sets')->where('shop_id', $this->shop_id)->where(['name' => 'serial'])->value('is_checked');
                $batch = Db::name('kuyou_sets')->where('shop_id', $this->shop_id)->where(['name' => 'batch'])->value('is_checked');
                $data = [];
                $number = 0;
                $all_money = 0;
                $finish_number = 0;
                foreach ($params['goods_code'] as $k => $v) {
                    if (!empty($v)) {//有产品编号
                        $number += (float)$params['need_number'][$k];
                        $all_money += (float)$params['all_money'][$k];
                        $goods = Db::name('kuyou_goods')->where('shop_id', $this->shop_id)->where(['goods_code' => $v])->field('id,goods_name,serial,batch,auxiliary')->find();
                        if (!isset($params['stores_id'][$k]) || empty($params['stores_id'][$k])) {
                            $this->error('请选择' . $goods['goods_name'] . '仓库');
                        }
                        if ($goods['auxiliary'] == 1 && empty($params['goods_attr'][$k])) {
                            $this->error('请选择' . $goods['goods_name'] . '属性');
                        }
                        if ($goods['batch'] == 1 && $batch == 1) {
                            if (empty($params['batch'][$k])) {
                                $this->error('请选择' . $goods['goods_name'] . '生产批次');
                            }
                        }

                        $serial_code = [];
                        if (isset($params['period_period'][$k]) && $goods['serial'] == 1 && $set == 1) {
                            if (empty($params['period_period'][$k])) {
                                $this->error('请输入' . $goods['goods_name'] . '商品序列号');
                            }
                            $period_period = unserialize($params['period_period'][$k]);
                            foreach ($period_period as $period_period_k => $period_period_v) {
                                $serial_code[] = $period_period_v['serial'];
                            }
                            $where = [];
                            $where['serial'] = ['in', $serial_code];
                            if (!empty($params['purchase_id'][$k])) {
                                $where['purchase_code'] = $params['purchase_id'][$k];
                            }
                            $goods_serial = Db::name('kuyou_goods_serial')->where('shop_id', $this->shop_id)->where($where)->column('serial');
                            if (!empty($goods_serial)) {
                                $this->error($goods['goods_name'] . '商品序列号已使用');
                            }
                            $goods_serials = Db::name('kuyou_goods_serial')->where('shop_id', $this->shop_id)->where(['store_status' => '1', 'serial' => ['in', $serial_code]])->column('serial');
                            if (!empty($goods_serials)) {
                                $goods_serials = implode(',', $goods_serials);
                                $this->error('商品序列号' . $goods_serials . '已经在库，不可重复入库');
                            }
                        }

                        $finish_number += (float)$params['number'][$k];
                        $bad_number = ((float)$params['need_number'][$k]) - ((float)$params['number'][$k]);
                        $bad_number = $bad_number < 0 ? 0 : $bad_number;
                        $data[] = [
                            'shop_id' => $this->shop_id,
                            'type' => 2,
                            'goods_id' => $goods['id'],
                            'goods_code' => $v,
                            'goods_name' => $goods['goods_name'],
                            'goods_note' => $params['goods_note'][$k],
                            'unit_id' => (float)$params['goods_unit'][$k],
                            'default_unit' => (float)$params['default_unit'][$k],
                            'purchase' => (float)$params['unit_price'][$k],
                            'number' => (float)$params['number'][$k],
                            'basic_nmber' => (float)$params['basic_nmber'][$k],
                            'money' => (float)$params['all_money'][$k],
                            'stores_id' => (float)$params['stores_id'][$k],
                            'need_number' => (float)$params['need_number'][$k],
                            'bad_number' => $bad_number,
                            'goods_attr' => (float)$params['goods_attr'][$k],
                            'remark' => $params['remark'][$k],
                            'batch_id' => !empty($params['batch_id'][$k]) ? $params['batch_id'][$k] : null,
                            'batch_name' => !empty($params['batch'][$k]) ? $params['batch'][$k] : null,
                            'manufacture' => !empty($params['manufacture'][$k]) ? strtotime($params['manufacture'][$k]) : null,
                            'guarantee' => !empty($params['guarantee'][$k]) ? $params['guarantee'][$k] : null,
                            'validity' => !empty(strtotime($params['validity'][$k])) ? strtotime($params['validity'][$k]) : null,
                            'period' => $params['period_period'][$k] === '' ? null : $params['period_period'][$k],
                            'createtime' => time(),
                            'updatetime' => time(),
                        ];
                    }
                }

                if ($code_type == 1) {
                    $plan = Db::name('kuyou_production')->where('shop_id', $this->shop_id)->where(['purchase_code' => $params['chase_code']])->find();
                    if (empty($plan)) {
                        $this->error('生产单查询失败');
                    }
                } else {
                    $plan = Db::name('kuyou_outsourcing')->where('shop_id', $this->shop_id)->where(['purchase_code' => $params['chase_code']])->find();
                    if (empty($plan)) {
                        $this->error('委外加工单查询失败');
                    }
                }
                if (empty($data)) {
                    $this->error('请填写详情数据');
                } else {
                    $admin = Session::get('admin');
                    $code_type = isset($params['code_type']) ? $params['code_type'] : 1;
                    $code_type = $code_type == 2 ? 2 : 1;
                    $chase_code = $params['chase_code'];
                    if ($code_type == 1) {
                        $production_code = $params['chase_code'];
                        $outsourcing_code = '';
                    } else {
                        $production_code = '';
                        $outsourcing_code = $params['chase_code'];
                    }
                    $rowdata = [];
                    $rowdata['shop_id'] = $this->shop_id;
                    $rowdata['code_type'] = $code_type;
                    $rowdata['chase_code'] = $chase_code;
                    $rowdata['production_code'] = $production_code;
                    $rowdata['outsourcing_code'] = $outsourcing_code;
                    $rowdata['purchase_code'] = $params['purchase_code'];
                    $rowdata['purchase_time'] = strtotime($params['purchase_time']);
                    $rowdata['remark'] = $params['note'];
                    $rowdata['work_id'] = $params['work_id'];
                    $rowdata['number'] = $number;
                    $rowdata['all_money'] = $all_money;
                    $rowdata['finish_number'] = $finish_number;
                    $rowdata['examine_status'] = '0';
                    $rowdata['updatetime'] = time();
                    $rowdata['preparation_id'] = $admin['id'];
                    $rowdata['operation_id'] = $admin['id'];
                    if ($this->model->where('shop_id', $this->shop_id)->where(['purchase_code' => trim($params['purchase_code']), 'id' => ['<>', $ids]])->find()) {
                        $this->error('单据编号已占用');
                    }
                }


                $result = false;
                Db::startTrans();
                try {
                    if ($examine != 1 && $row['examine_status'] == 1) {
                        $this->examine($ids, 2);
                    }

                    if (!empty($rowdata)) {
                        $result = $this->model->where(['id' => $row['id']])->update($rowdata);
                    }

                    foreach ($data as $data_k => $data_v) {
                        $data[$data_k]['production_id'] = $row['id'];
                    }
                    Db::name('kuyou_acceptance_info')->where('shop_id', $this->shop_id)->where(['production_id' => $row['id']])->delete();
                    Db::name('kuyou_acceptance_info')->insertAll($data);
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    if ($examine != 1) {
                        try {
                            $exa = $this->examine($ids, 1);
                        } catch (Exception $e) {
                        }
                        if (!isset($exa) || $exa['status'] != 1) {
                            $this->error(__('No rows were inserted'));
                        }
                    }
                    $this->success('编辑成功');
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $purchase_info = Db::name('kuyou_acceptance_info')->where('shop_id', $this->shop_id)->where(['production_id' => $row['id']])->select();
        foreach ($purchase_info as $purchase_info_k => $purchase_info_v) {
            $purchase_info[$purchase_info_k]['unit'] = json_decode((new GoodsPurchase())->getUnit($purchase_info_v['goods_code']), true);
        }
        $this->view->assign("purchase_info", $purchase_info);

        $this->view->assign("row", $row);

        //审核禁止输入
        $examine_set = Db::name('kuyou_sets')->where('shop_id', $this->shop_id)->where(['name' => 'examine'])->value('is_checked');
        if ($examine_set == 1 && $row['examine_status'] == 1) {
            $this->assign('exclass', 'hiddens');
        }
        $this->assignconfig("examine_status", $row['examine_status']);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        if ($ids) {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $this->model->where('shop_id', $this->shop_id)->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where('shop_id', $this->shop_id)->where($pk, 'in', $ids)->select();
            $examine = Db::name('kuyou_sets')->where('shop_id', $this->shop_id)->where(['name' => 'examine'])->value('is_checked');
            $count = 0;
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {

                    if ($v['examine_status'] == 1 && $examine == 1) {
                        exception('不能删除已审核的数据');
                        return;
                    } elseif ($v['examine_status'] == 1 && $examine != 1) {
                        try {
                            $this->examine($v['id'], 2);
                        } catch (Exception $e) {
                        }
                    }

                    $count += $v->delete();
                    Db::name('kuyou_acceptance_info')->where('shop_id', $this->shop_id)->where(['production_id' => $v['id']])->delete();
                }
                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

    //审核/反审核
    public function examine($ids = null, $examine = 1)
    {
        $examine_set = Db::name('kuyou_sets')->where('shop_id', $this->shop_id)->where(['name' => 'examine'])->value('is_checked');
        if (empty($ids)) {
            if ($examine_set != 1) {
                return ['status' => 0, 'msg' => '系统错误'];
            }
            $this->error('请选择要操作的数据');
        }
        $serial_set = Db::name('kuyou_sets')->where('shop_id', $this->shop_id)->where((new Goods())->setWhere(['shop_id' => $this->shop_id, 'name' => 'serial']))->value('is_checked');//序列号设置
        $batch_set = Db::name('kuyou_sets')->where('shop_id', $this->shop_id)->where((new Goods())->setWhere(['shop_id' => $this->shop_id, 'name' => 'batch']))->value('is_checked');//批次设置
        $admin = Session::get('admin');
        if ($examine == 1) {
            $list = $this->model->where('shop_id', $this->shop_id)->where(['id' => ['in', $ids], 'examine_status' => '0'])->select();


            if (empty($list)) {
                if ($examine_set != 1) {
                    return ['status' => 0, 'msg' => '系统错误'];
                }
                $this->error('请选择需要审核的数据');
            }
            $production_id = $this->model->where('shop_id', $this->shop_id)->where(['id' => ['in', $ids], 'examine_status' => '0'])->column('id');
            $production_code = [];
            $outsourcing_code = [];
            foreach ($list as $list_k => $list_v) {
                if ($list_v['code_type'] == 1) {
                    $production_code[] = $list_v['production_code'];
                } else {
                    $outsourcing_code[] = $list_v['outsourcing_code'];
                }
            }

            $purchase_info_id = Db::name('kuyou_acceptance_info')->where('shop_id', $this->shop_id)->where(['production_id' => ['in', $production_id]])->column('id');
            $info_list = Db::name('kuyou_acceptance_info')->where('shop_id', $this->shop_id)->where(['production_id' => ['in', $production_id]])->select();

            $addstock = [];//库存表添加库存
            $editstock = [];
            $goods_stock = [];
            $purchase_info = [];
            $goods_stock_log = [];//收发详情
            $serial_log = [];//序列号跟踪
            $serial_data = [];

            $goods_batch_add = [];//批次库存新增
            $goods_batch_edit = [];//批次库存更新
            $goods_batch_log = [];//批次记录
            $per_code = [];//序列号
            $goods_serial = [];//序列号添加
            $edit_serial = [];//序列号更新
            foreach ($info_list as $list_k => $list_v) {
                $picking = Db::name('kuyou_acceptance')->where('shop_id', $this->shop_id)->where(['id' => $list_v['production_id']])->find();

                $info_list[$list_k]['sales_order_id'] = $list_v['id'];
                $info_list[$list_k]['createtime'] = time();
                $info_list[$list_k]['updatetime'] = time();
                $stock_number = $list_v['basic_nmber'];
                $goods = Db::name('kuyou_goods')->where('shop_id', $this->shop_id)->where(['goods_code' => $list_v['goods_code']])->field('id,attribute_id,inventory,unit_id,goods_name,batch,serial,goods_code')->find();
                $status = false;
                foreach ($goods_stock as $goods_stock_k => $goods_stock_v) {
                    if ($goods_stock_v['id'] == $goods['id']) {
                        $goods_stock[$goods_stock_k]['inventory'] = $goods_stock_v['inventory'] + $stock_number;
                        $status = true;
                    }
                }
                if (!$status) {
                    $goods_stock[] = [
                        'id' => $goods['id'],
                        'inventory' => $goods['inventory'] + $stock_number,
                    ];
                }
                //商品库存
                $where = [];
                $where['goods_code'] = $list_v['goods_code'];
                $where['stores_id'] = $list_v['stores_id'];
                $goods_attr = null;
                if ($list_v['goods_attr'] > 0) {
                    $goods_attr = $list_v['goods_attr'];
                    if ($goods_attr > 0) {
                        $where['attribute_id'] = $goods_attr;
                    }
                }

                //获取成本单价
                $balance_cost = 0;
                $balance_unit = 0;
                $goodsInfo = json_decode((new GoodsPurchase())->getUnit($list_v['goods_code'], $list_v['stores_id'], $list_v['goods_attr']), true);
                $unitData = isset($goodsInfo['unit']) ? $goodsInfo['unit'] : [];
                if (empty($unitData)) {
                    if ($examine_set != 1) {
                        return ['status' => 0, 'msg' => '商品信息获取错误'];
                    }
                    $this->error('商品信息获取错误');
                } else {
                    foreach ($unitData as $unitData_k => $unitData_v) {
                        if ($unitData_v['id'] == $goods['unit_id']) {
                            $balance_unit = $unitData_v['purchase'];
                        }
                    }
                }
                //获取成本单价结束

                $stock = Db::name('kuyou_goods_stock')->where('shop_id', $this->shop_id)->where($where)->field('stock,id')->find();
                if ($stock) {
                    $status = false;
                    foreach ($editstock as $editstock_k => $editstock_v) {
                        if ($editstock_v['id'] == $stock['id']) {
                            $editstock[$editstock_k]['stock'] += $stock_number;
                            $status = true;
                            $balance_cost = (round($balance_unit * $stock_number, 4));
                        }
                    }
                    if (!$status) {
                        $editstock[] = [
                            'id' => $stock['id'],
                            'stock' => (round($stock['stock'] + $stock_number, 4))
                        ];
                        $balance_cost = (round($balance_unit * $stock_number, 4));
                    }
                } else {
                    $status = false;
                    foreach ($addstock as $addstock_k => $addstock_v) {
                        if ($addstock_v['goods_code'] == $list_v['goods_code'] && $addstock_v['attribute_id'] == $goods_attr && $addstock_v['stores_id'] == $list_v['stores_id']) {
                            $addstock[$addstock_k]['stock'] += $stock_number;
                            $status = true;
                        }
                    }
                    if (!$status) {
                        $addstock[] = (new Goods())->setDataInfo([
                            'shop_id' => $this->shop_id,
                            'goods_id' => isset($goods['id']) ? $goods['id'] : '',
                            'goods_code' => $list_v['goods_code'],
                            'attribute_id' => isset($goods_attr) ? $goods_attr : 0,
                            'stores_id' => $list_v['stores_id'],
                            'stock' => $stock_number,
                            'createtime' => time(),
                            'updatetime' => time(),
                        ]);
                    }
                    $balance_cost = (round($balance_unit * $stock_number, 4));
                }
                $purchase_info[] = $list_v['id'];

                //收发详情
                if ($goods) {
                    $goods_stock_log[] = (new Goods())->setDataInfo([
                        'shop_id' => $this->shop_id,
                        'purchase_code' => $picking['purchase_code'],
                        'purchase_id' => $list_v['id'],
                        'purchase_time' => $picking['purchase_time'],
                        'business_type' => '17',
                        'customer_id' => null,
                        'goods_id' => $goods['id'],
                        'goods_code' => $list_v['goods_code'],
                        'attribute_id' => (float)$list_v['goods_attr'],
                        'goods_unit' => (float)$list_v['unit_id'],
                        'stores_id' => (float)$list_v['stores_id'],
                        'remark' => $list_v['remark'],
                        'enter_number' => (float)$list_v['number'],
                        'enter_basics' => (float)$list_v['basic_nmber'],
                        'enter_unit' => $balance_unit,
                        'enter_cost' => $balance_cost,
                        'createtime' => time(),
                        'updatetime' => time(),
                    ]);
                }
                //收发详情结束
                //批次跟踪
                if ($batch_set == 1 && $goods['batch'] == 1) {
                    $where = [];
                    $where['goods_id'] = $goods['id'];
                    if (isset($goods_attr) && $goods_attr > 0) {
                        $where['attribute_id'] = $goods_attr;
                    }
                    $where['stores_id'] = $list_v['stores_id'];
                    if (empty($list_v['batch_id'])) {
                        $list_v['batch_id'] = Db::name('kuyou_goods_batch')->where('shop_id', $this->shop_id)->where((new Goods())->setWhere(
                            [
                                'shop_id' => $this->shop_id,
                                'goods_id' => $goods['id'],
                                'attribute_id' => isset($goods_attr) ? (float)$goods_attr : 0,
                                'stores_id' => $list_v['stores_id'],
                                'batch_name' => $list_v['batch_name'],
                            ]
                        ))->value('id');
                        if (!empty($list_v['batch_id'])) {
                            $edit_batch[] = ['id' => $list_v['id'], 'batch_id' => $list_v['batch_id']];
                        }
                    }
                    $where['batch_id'] = $list_v['batch_id'];
                    $where['shop_id'] = $this->shop_id;
                    $where = (new Goods())->setWhere($where);
                    $goods_batch_stock = Db::name('kuyou_goods_batch_stock')->where('shop_id', $this->shop_id)->where($where)->find();
                    if (empty($goods_batch_stock)) {
                        $status = false;
                        foreach ($goods_batch_add as $goods_batch_add_k => $goods_batch_add_v) {
                            if ($goods_batch_add_v['batch_id'] == $list_v['batch_id'] && $goods_batch_add_v['goods_id'] == $list_v['goods_id'] && $goods_batch_add_v['attribute_id'] == $goods_attr && $goods_batch_add_v['stores_id'] == $list_v['stores_id']) {
                                $goods_batch_add[$goods_batch_add_k]['stock'] += $stock_number;
                                $status = true;
                            }
                        }
                        if (!$status) {
                            if (empty($list_v['batch_id'])) {
                                $batch_id = Db::name('kuyou_goods_batch')->insertGetId((new Goods())->setDataInfo([
                                    'shop_id' => $this->shop_id,
                                    'goods_id' => $goods['id'],
                                    'goods_code' => $goods['goods_code'],
                                    'attribute_id' => isset($goods_attr) ? (float)$goods_attr : 0,
                                    'stores_id' => $list_v['stores_id'],
                                    'batch_name' => $list_v['batch_name'],
                                    'manufacture' => $list_v['manufacture'],
                                    'guarantee' => $list_v['guarantee'],
                                    'validity' => $list_v['validity'],
                                    'createtime' => time(),
                                    'updatetime' => time(),
                                ]));
                            } else {
                                $batch_id = $list_v['batch_id'];
                            }
                            $edit_batch[] = ['id' => $list_v['id'], 'batch_id' => $batch_id];
                            $goods_batch_add[] = (new Goods())->setDataInfo([
                                'shop_id' => $this->shop_id,
                                'batch_id' => $batch_id,
                                'goods_id' => $goods['id'],
                                'goods_code' => $goods['goods_code'],
                                'attribute_id' => isset($goods_attr) ? (float)$goods_attr : 0,
                                'stores_id' => $list_v['stores_id'],
                                'stock' => $stock_number,
                                'createtime' => time(),
                                'updatetime' => time(),
                            ]);
                        }
                    } else {
                        if (isset($goods_batch_edit[$goods_batch_stock['id']])) {
                            $goods_batch_edit[$goods_batch_stock['id']] += $stock_number;
                        } else {
                            $goods_batch_edit[$goods_batch_stock['id']] = $goods_batch_stock['stock'] + $stock_number;
                        }
                    }
                    //商品批次跟踪
                    $goods_batch_log[] = (new Goods())->setDataInfo([
                        'shop_id' => $this->shop_id,
                        'goods_id' => $goods['id'],
                        'goods_unit' => $list_v['unit_id'],
                        'stores_id' => $list_v['stores_id'],
                        'batch_id' => (float)$list_v['batch_id'],
                        'number' => (float)$list_v['basic_nmber'],
                        'purchase_code' => $picking['purchase_code'],
                        'purchase_time' => $picking['purchase_time'],
                        'business_type' => '17',
                        'remarks' => $list_v['remark'],
                        'order_id' => $list_v['id'],
                        'unit_price' => $list_v['purchase'],
                        'all_money' => $list_v['money'],
                        'money_tax_price' => $list_v['money'],
                        'createtime' => time(),
                        'updatetime' => time(),
                    ]);
                    //商品批次跟踪结束
                }

                //序列号跟踪
                if ($goods && $goods['serial'] == 1 && $serial_set == 1 && !empty($list_v['period'])) {
                    $period = unserialize($list_v['period']);
                    foreach ($period as $period_k => $period_v) {

                        $per_s = Db::name('kuyou_goods_serial')->where('shop_id', $this->shop_id)->where((new Goods())->setWhere(['serial' => $period_v['serial']]))->field('serial,status,store_status')->find();//序列号
                        if ($per_s) {//仓库有记录
                            $edit_serial[] = ['serial' => $per_s['serial'], 'assemble_id' => $list_v['id'], 'type' => '1', 'store_status' => 1, 'old_status' => $per_s['status'], 'old_store' => $per_s['store_status']];
                        } else {
                            if (!in_array($period_v['serial'], $per_code)) {
                                $per_code[] = $period_v['serial'];
                            }
                            $goods_serial[] = (new Goods())->setDataInfo([
                                'shop_id' => $this->shop_id,
                                'purchase_id' => $list_v['id'],
                                'purchase_code' => $picking['purchase_code'],
                                'goods_id' => $goods['id'],
                                'goods_code' => $goods['goods_code'],
                                'stores_id' => $list_v['stores_id'],
                                'attribute_id' => isset($goods_attr) ? (float)$goods_attr : 0,
                                'serial' => $period_v['serial'],
                                'remark' => $period_v['remark'],
                                'status' => '15',
                                'store_status' => '1',
                                'order_id' => $list_v['id'],
                                'createtime' => time(),
                                'updatetime' => time(),
                            ]);
                        }
                        $serial_log[] = (new Goods())->setDataInfo([
                            'shop_id' => $this->shop_id,
                            'goods_id' => $goods['id'],
                            'stores_id' => $list_v['stores_id'],
                            'attribute_id' => isset($goods_attr) ? (float)$goods_attr : 0,
                            'serial' => $period_v['serial'],
                            'business_type' => '17',
                            'store_status' => '1',
                            'remarks' => $period_v['remark'],
                            'purchase_id' => $list_v['id'],
                            'purchase_code' => $picking['purchase_code'],
                            'purchase_time' => $picking['purchase_time'],
                            'order_id' => $list_v['id'],
                            'createtime' => time(),
                            'updatetime' => time(),
                        ]);
                    }
                }
                //序列号跟踪
            }
            foreach ($editstock as $editstock_k => $editstock_v) {
                if ($editstock_v['stock'] < 0) {
                    $store = Db::name('kuyou_stores')->where('shop_id', $this->shop_id)->where(['id' => $list_v['stores_id']])->value('name');
                    if ($examine_set != 1) {
                        return ['status' => 0, 'msg' => $goods['goods_name'] . '库存不足,操作失败'];
                    }
                    $this->error($store . '库存不足,操作失败');
                }
            }

            Db::startTrans();
            try {
                if (!empty($production_code)) {
                    Db::name('kuyou_production')->where('shop_id', $this->shop_id)->where(['purchase_code' => ['in', $production_code]])->update(['status' => '1']);
                }
                if (!empty($outsourcing_code)) {
                    Db::name('kuyou_outsourcing')->where('shop_id', $this->shop_id)->where(['purchase_code' => ['in', $outsourcing_code]])->update(['status' => '1']);
                }
                if (!empty($goods_stock_log)) {
                    Db::name('kuyou_goods_log')->insertAll($goods_stock_log);
                }
                //批次库存新增
                if (!empty($goods_batch_add)) {
                    Db::name('kuyou_goods_batch_stock')->insertAll($goods_batch_add);
                }
                //批次库存更新
                if (!empty($goods_batch_edit)) {
                    foreach ($goods_batch_edit as $k => $v) {
                        Db::name('kuyou_goods_batch_stock')->where('shop_id', $this->shop_id)->where((new Goods())->setWhere(['id' => $k]))->update(['stock' => $v]);
                    }
                }
                //批次记录
                if (!empty($goods_batch_log)) {
                    Db::name('kuyou_goods_batch_log')->insertAll($goods_batch_log);
                }
                //序列号添加
                if (!empty($goods_serial)) {
                    Db::name('kuyou_goods_serial')->insertAll($goods_serial);
                }
                //序列号更新
                if (!empty($edit_serial)) {
                    foreach ($edit_serial as $edit_serial_k => $edit_serial_v) {
                        Db::name('kuyou_goods_serial')->where('shop_id', $this->shop_id)->where((new Goods())->setWhere(['shop_id' => $this->shop_id, 'serial' => $edit_serial_v['serial']]))->update([
                            'assemble_id' => $edit_serial_v['assemble_id'],
                            'status' => $edit_serial_v['type'],
                            'store_status' => $edit_serial_v['store_status'],
                            'old_status' => $edit_serial_v['old_status'],
                            'old_store' => $edit_serial_v['old_store']
                        ]);
                    }
                }
                //序列号跟踪
                if (!empty($serial_log)) {
                    Db::name('kuyou_goods_serial_log')->insertAll($serial_log);
                }

                if (!empty($serial_data)) {
                    (new \app\admin\model\kuyou\GoodsSerial())->saveAll($serial_data);
                }
                if (!empty($goods_batch_info)) {
                    Db::name('kuyou_goods_batch_sale_info')->insertAll($goods_batch_info);
                }
                //库存表添加库存
                if (!empty($addstock)) {
                    Db::name('kuyou_goods_stock')->insertAll($addstock);
                }
                if (!empty($editstock)) {
                    (new \app\admin\model\kuyou\GoodsStock())->saveAll($editstock);
                }
                if (!empty($goods_stock)) {
                    (new \app\admin\model\kuyou\Goods())->saveAll($goods_stock);
                }
                $result = $this->model->where(['id' => ['in', $ids], 'examine_status' => '0'])
                    ->update(['examine_status' => 1, 'examine_id' => $admin['id'], 'examine_time' => time(), 'operation_id' => $admin['id']]);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if (isset($result) && $result !== false) {
                if ($examine_set != 1) {
                    return ['status' => 1, 'msg' => '操作成功'];
                }
                $this->success(sizeof($list) . '条数据审核成功');
            } else {
                if ($examine_set != 1) {
                    return ['status' => 0, 'msg' => '操作失败'];
                }
                $this->error('审核失败');
            }
        } else {
            $list = $this->model->where('shop_id', $this->shop_id)->where(['id' => ['in', $ids], 'examine_status' => '1'])->select();
            $production_id = $this->model->where('shop_id', $this->shop_id)->where(['id' => ['in', $ids], 'examine_status' => '1'])->column('id');
            $purchase_code = $this->model->where('shop_id', $this->shop_id)->where(['id' => ['in', $ids], 'examine_status' => '1'])->column('purchase_code');
            foreach ($list as $list_k => $list_v) {
                if ($list_v['code_type'] == 1) {
                    $production_code[] = $list_v['production_code'];
                } else {
                    $outsourcing_code[] = $list_v['outsourcing_code'];
                }
            }

            //获取设置
            $set = \think\Db::name('kuyou_sets')->where('shop_id', $this->shop_id)->field('type,name,value,is_checked,set')->select();
            $setList = [];
            foreach ($set as $set_k => $set_v) {
                if (!empty($set_v['set'])) {
                    $set_v['set'] = unserialize($set_v['set']);
                }
                $setList[$set_v['name']] = $set_v;
            }
            //数量价格格式化
            $number_decimal = $setList['number_decimal']['value'];

            $purchase_info_id = Db::name('kuyou_acceptance_info')->where('shop_id', $this->shop_id)->where(['production_id' => ['in', $production_id]])->column('id');
            $info_list = Db::name('kuyou_acceptance_info')->where('shop_id', $this->shop_id)->where(['production_id' => ['in', $production_id]])->select();

            $editstock = [];
            $goods_stock = [];
            $purchase_info = [];
            $serial_data = [];
            foreach ($info_list as $list_k => $list_v) {
                $picking = Db::name('kuyou_acceptance')->where('shop_id', $this->shop_id)->where(['id' => $list_v['production_id']])->find();
                if (!empty($list_v['period'])) {
                    $goods_serial = unserialize($list_v['period']);
                    foreach ($goods_serial as $goods_serial_k => $goods_serial_v) {
                        $this_serial = Db::name('kuyou_goods_serial')->where('shop_id', $this->shop_id)->where(['serial' => $goods_serial_v['serial']])->find();
                        if (empty($this_serial)) {
                            if ($examine_set != 1) {
                                return ['status' => 0, 'msg' => '序列号' . $goods_serial_v['serial'] . '不存在'];
                            }
                            $this->error('序列号' . $goods_serial_v['serial'] . '不存在');
                        } elseif ($this_serial['store_status'] != 1) {
                            $this->error('序列号' . $goods_serial_v['serial'] . '已出库');
                        }
                        $serial_data[] = [
                            'id' => $this_serial['id'],
                            'sale_id' => $picking['id'],
                            'sale_code' => $picking['purchase_code'],
                            'status' => $this_serial['old_status'],
                            'store_status' => $this_serial['old_store'],
                        ];
                    }
                }

                $info_list[$list_k]['sales_order_id'] = $list_v['id'];
                $info_list[$list_k]['createtime'] = time();
                $info_list[$list_k]['updatetime'] = time();
                $stock_number = $list_v['basic_nmber'];
                $goods = Db::name('kuyou_goods')->where('shop_id', $this->shop_id)->where(['goods_code' => $list_v['goods_code']])->field('id,attribute_id,inventory,unit_id,goods_name')->find();
                $status = false;
                foreach ($goods_stock as $goods_stock_k => $goods_stock_v) {
                    if ($goods_stock_v['id'] == $goods['id']) {
                        $goods_stock[$goods_stock_k]['inventory'] = $goods_stock_v['inventory'] - $stock_number;
                        $status = true;
                    }
                }
                if (!$status) {
                    $goods_stock[] = [
                        'id' => $goods['id'],
                        'inventory' => $goods['inventory'] - $stock_number,
                    ];
                }
                //商品库存
                $where = [];
                $where['goods_code'] = $list_v['goods_code'];
                $where['stores_id'] = $list_v['stores_id'];
                $goods_attr = null;
                if ($list_v['goods_attr'] > 0) {
                    $goods_attr = $list_v['goods_attr'];
                    if ($goods_attr > 0) {
                        $where['attribute_id'] = $goods_attr;
                    }
                }

                $stock = Db::name('kuyou_goods_stock')->where('shop_id', $this->shop_id)->where($where)->field('stock,id')->find();
                if ($stock) {
                    $status = false;
                    foreach ($editstock as $editstock_k => $editstock_v) {
                        if ($editstock_v['id'] == $stock['id']) {
                            $editstock[$editstock_k]['stock'] -= $stock_number;
                            $status = true;
                        }
                    }
                    if (!$status) {
                        $editstock[] = [
                            'id' => $stock['id'],
                            'stock' => (round($stock['stock'] - $stock_number, 4))
                        ];
                    }
                } else {
                    if ($examine_set != 1) {
                        return ['status' => 0, 'msg' => '库存数据错误，操作失败'];
                    }
                    $this->error('库存数据错误,反审核失败');
                }
                $purchase_info[] = $list_v['id'];
            }
            foreach ($editstock as $editstock_k => $editstock_v) {
                if ($editstock_v['stock'] < 0) {
                    $store = Db::name('kuyou_stores')->where('shop_id', $this->shop_id)->where(['id' => $list_v['stores_id']])->value('name');
                    if ($examine_set != 1) {
                        return ['status' => 0, 'msg' => $goods['goods_name'] . '库存不足,操作失败'];
                    }
                    $this->error($store . '库存不足,操作失败');
                }
            }

            //批次库存数据
            $goods_serial = Db::name('kuyou_acceptance_info')->where('shop_id', $this->shop_id)->where(['id' => ['in', $purchase_info_id]])->select();
            $goods_batch_add = [];
            $goods_batch_edit = [];
            foreach ($goods_serial as $k => $v) {
                $goods = Db::name('kuyou_goods')->where('shop_id', $this->shop_id)->where((new Goods())->setWhere(['shop_id' => $this->shop_id, 'goods_code' => $v['goods_code']]))->field('id,goods_name,attribute_id,inventory,unit_id,batch')->find();
                if ($goods['batch'] == 1) {
                    //先通过单位计算需修改多单位商品的库存数量
                    if ($goods['unit_id'] == $v['unit_id']) {
                        $stock_number = $v['number'];
                    } else {
                        $unit_conversion = Db::name('kuyou_unit_conversion')->where('shop_id', $this->shop_id)->where((new Goods())->setWhere(['shop_id' => $this->shop_id, 'main_unit' => $goods['unit_id'], 'vice_unit' => $v['goods_unit']]))->value('advance_rate');
                        if (empty($unit_conversion)) {
                            if ($examine_set != 1) {
                                return ['status' => 0, 'msg' => '商品单位信息查询失败'];
                            }
                            $this->error('商品单位信息查询失败');
                        } else {
                            $stock_number = round(($v['number'] * $unit_conversion), $number_decimal);
                        }
                    }

                    $where = [];
                    $where['goods_id'] = $goods['id'];
                    if ($v['goods_attr']) {
                        $where['attribute_id'] = $v['goods_attr'];
                    }
                    $where['stores_id'] = $v['stores_id'];
                    if (empty($v['batch_id'])) {
                        $v['batch_id'] = Db::name('kuyou_goods_batch')->where('shop_id', $this->shop_id)->where((new Goods())->setWhere(
                            [
                                'shop_id' => $this->shop_id,
                                'goods_id' => $goods['id'],
                                'attribute_id' => isset($goods_attr) ? (float)$goods_attr : 0,
                                'stores_id' => $list_v['stores_id'],
                                'batch_name' => $list_v['batch_name'],
                            ]
                        ))->value('id');
                    }
                    $where['batch_id'] = $v['batch_id'];
                    $where['shop_id'] = $this->shop_id;
                    $where = (new Goods())->setWhere($where);
                    $goods_batch_stock = Db::name('kuyou_goods_batch_stock')->where('shop_id', $this->shop_id)->where($where)->find();
                    if (empty($goods_batch_stock)) {
                        if ($examine_set != 1) {
                            return ['status' => 0, 'msg' => '商品批次信息查询失败'];
                        }
                        $this->error('商品批次信息查询失败');
                    } else {
                        if (isset($goods_batch_edit[$goods_batch_stock['id']])) {
                            $goods_batch_edit[$goods_batch_stock['id']] -= $stock_number;
                        } else {
                            $goods_batch_edit[$goods_batch_stock['id']] = $goods_batch_stock['stock'] - $stock_number;
                        }
                    }
                }
            }
            //批次库存数据结束

            $admin = Session::get('admin');
            Db::startTrans();
            try {
                if (!empty($production_code)) {
                    Db::name('kuyou_production')->where('shop_id', $this->shop_id)->where(['purchase_code' => ['in', $production_code]])->update(['status' => '0']);
                }
                if (!empty($outsourcing_code)) {
                    Db::name('kuyou_outsourcing')->where('shop_id', $this->shop_id)->where(['purchase_code' => ['in', $outsourcing_code]])->update(['status' => '0']);
                }
                Db::name('kuyou_goods_serial')->where('shop_id', $this->shop_id)->where((new Goods())->setWhere(['shop_id' => $this->shop_id, 'purchase_code' => ['in', $purchase_code]]))->delete();
                //序列号跟踪
                if (!empty($purchase_info_id)) {
                    Db::name('kuyou_goods_serial_log')->where('shop_id', $this->shop_id)->where((new Goods())->setWhere(['shop_id' => $this->shop_id, 'business_type' => '17', 'purchase_id' => ['in', $purchase_info_id]]))->delete();
                }
                //批次库存数据
                //删除批次跟踪
                if (!empty($purchase_info_id)) {
                    Db::name('kuyou_goods_batch_log')->where('shop_id', $this->shop_id)->where((new Goods())->setWhere(['shop_id' => $this->shop_id, 'business_type' => '17', 'order_id' => ['in', $purchase_info_id]]))->delete();
                }
                if (!empty($goods_batch_edit)) {
                    foreach ($goods_batch_edit as $k => $v) {
                        Db::name('kuyou_goods_batch_stock')->where('shop_id', $this->shop_id)->where((new Goods())->setWhere(['id' => $k]))->update(['stock' => $v]);
                    }
                }
                //批次库存数据结束
                if (!empty($serial_data)) {
                    (new \app\admin\model\kuyou\GoodsSerial())->saveAll($serial_data);
                }
                if (!empty($purchase_info_id)) {
                    Db::name('kuyou_goods_batch_sale_info')->where('shop_id', $this->shop_id)->where(['purchase_info_id' => ['in', $purchase_info_id]])->delete();
                    Db::name('kuyou_goods_log')->where('shop_id', $this->shop_id)->where(['business_type' => '17', 'purchase_id' => ['in', $purchase_info_id]])->delete();
                }
                if (!empty($editstock)) {
                    (new \app\admin\model\kuyou\GoodsStock())->saveAll($editstock);
                }
                if (!empty($goods_stock)) {
                    (new \app\admin\model\kuyou\Goods())->saveAll($goods_stock);
                }
                $result = $this->model->where('shop_id', $this->shop_id)->where(['id' => ['in', $ids], 'examine_status' => '1'])->update(['examine_status' => '0', 'examine_id' => 0, 'examine_time' => '', 'operation_id' => $admin['id']]);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if (isset($result) && $result !== false) {
                if ($examine_set != 1) {
                    return ['status' => 1, 'msg' => '操作成功'];
                }
                $this->success(sizeof($list) . '条数据反审核成功');
            } else {
                if ($examine_set != 1) {
                    return ['status' => 0, 'msg' => '操作失败'];
                }
                $this->error('反审核失败');
            }
        }
    }

    public function pickinfo()
    {
        $params = input();
        $code_type = isset($params['code_type']) ? $params['code_type'] : 1;
        $code_type = $code_type == 2 ? 2 : 1;
        if ($code_type == 1) {
            $plan = Db::name('kuyou_production')->where('shop_id', $this->shop_id)->where(['purchase_code' => $params['purchase_code']])->find();
            if ($plan) {
                $info = Db::name('kuyou_production_info')->where('shop_id', $this->shop_id)->where(['production_id' => $plan['id'], 'type' => '2'])->select();
                $result = array("total" => sizeof($info), "rows" => $info);
                return json($result);
            } else {
                $this->error('查询失败');
            }
        } else {
            $plan = Db::name('kuyou_outsourcing')->where('shop_id', $this->shop_id)->where(['purchase_code' => $params['purchase_code']])->find();
            if ($plan) {
                $production_id = Db::name('kuyou_plan')->where('shop_id', $this->shop_id)->where(['purchase_code' => $plan['plan_code']])->value('id');
                $info = Db::name('kuyou_plan_info')->where('shop_id', $this->shop_id)->where(['plan_id' => $production_id])->select();
                $result = array("total" => sizeof($info), "rows" => $info);
                return json($result);
            } else {
                $this->error('查询失败');
            }
        }
    }

}
